package com.bocai.dahuatech.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.utils.AESUtils;
import com.bumptech.glide.Glide;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PromotionActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.iv_promotion_home)
    ImageView ivHome;
    @Bind(R.id.ll_promotion_dismiss)
    LinearLayout llDismiss;
    @Override
    protected int contentView() {
        return R.layout.activity_promotion;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        getPic();
        initEvent();
    }

    private void getPic() {
        getTestApi().getPromotionPic()
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        if(baseBean.getReturnNo().equals("1"))
                        {
                            String result= AESUtils.decode(baseBean.getContent());
                            Log.e("result",result);
                            //http://fx.dahuatech.com:800/DHApp/Upload/Images/17/6a1a404aa9ec4c46b9bb8254f485438c.jpg
                            result=result.substring(1,result.length()-1);
                            Glide.with(PromotionActivity.this).load(result).into(ivHome);

                        }
                    }
                });
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivHome.setOnClickListener(this);
        llDismiss.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.iv_promotion_home:
                Intent intent=new Intent(this,PromotionProductActivity.class);
                intent.putExtra("what","promotion");
                startActivity(intent);
                finish();
                break;
            case R.id.ll_promotion_dismiss:
                startActivity(new Intent(this,HomeActivity.class));
                finish();
                break;
        }
    }
}
